<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@page import="java.util.Collection"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>
<%@page import="org.crud4j.core.CrudField"%>
<%@page import="org.crud4j.core.CrudService"%>
<%@page import="org.crud4j.portlet.CrudContextKey"%>
<%@page import="org.crud4j.core.CrudBean"%>
<%@page import="org.crud4j.core.CrudFieldDef"%>
<portlet:defineObjects/>
<%
CrudService service = (CrudService) request.getAttribute(CrudContextKey.CRUD_SERVICE.getKey());
CrudField field = (CrudField) request.getAttribute("field") ;
CrudBean fk = null ;
if ((field.getDef().isCrudBean() || field.getDef().isCrudFk()) && field.getValue() != null) {
	Class type = null ;
	if (field.getDef().isCrudBean()) {
		type = field.getDef().getType() ;
	} else {
		type = field.getDef().getCrudFkClass() ;		
	}
	
    CrudBean fkBean = service.create(type) ;
	CrudFieldDef fkIdCrudFieldDef = (CrudFieldDef) fkBean.getDef().getIds().iterator().next() ;
	
	Map params = new HashMap();
	params.put(fkIdCrudFieldDef.getName(), field.getValue().toString());
	
	fk = service.find(type, params);
}
%>
<c:if test="${field.def.crudBean and not empty field.value}">
<%= fk.getLabel() %>
</c:if>
<c:if test="${!field.def.crudBean}">
	<c:if test="${not empty(field.def.crudFkClass)}">
		<%= fk.getLabel() %>	
	</c:if>
<!-- Simple value -->
	<c:if test="${empty(field.def.crudFkClass)}">
		<c:out value="${field.value}"/>		
	</c:if>
</c:if>
